import React from 'react';
import logo from '@/assets/icons/ai-logo-eai.svg';
import logo1 from '@/assets/icons/ai-logo1.svg';
import logo2 from '@/assets/icons/ai-logo2.svg';

import agentLogo1 from '@/assets/icons/ai-agent1.svg';
import agentLogo2 from '@/assets/icons/ai-agent2.svg';
import agentLogo3 from '@/assets/icons/ai-agent3.svg';
import human from '@/assets/icons/human.svg';
import workflowLogo1 from '@/assets/icons/ai-workflow1.svg';
import workflowLogo2 from '@/assets/icons/ai-workflow2.svg';
import workflowLogo3 from '@/assets/icons/ai-workflow3.svg';

import mcpLogo1 from '@/assets/icons/mcpLogo1.svg';
import mcpLogo2 from '@/assets/icons/mcpLogo2.svg';
import mcpLogo3 from '@/assets/icons/mcpLogo3.svg';

import fusionLogo1 from '@/assets/icons/ai-fusion1.svg';
import fusionLogo2 from '@/assets/icons/ai-fusion2.svg';
import fusionLogo3 from '@/assets/icons/ai-fusion3.svg';

import firecrawl from '@/assets/icons/firecrawl.svg';
import amap from '@/assets/icons/amap.svg';
import baidumap from '@/assets/icons/baidumap.svg';
import tencentmap from '@/assets/icons/tencentmap.svg';
import deepwiki from '@/assets/icons/deepwiki.svg';
import gitmcp from '@/assets/icons/gitmcp.svg';
import huggingface from '@/assets/icons/huggingface.svg';



import aiaalogo from '@/assets/images/aiaaLogo.png';

import homeback from '@/assets/images/background/homepagebg.svg';
import entImg from '@/assets/images/background/ent.svg';
import medImg from '@/assets/images/background/med.svg';
import aiaaImg from '@/assets/images/background/aiaa.png';

import { SvgIcon } from '@/components/icon';
import { getFilenameFromPath } from '@/utils/common';
import { useOrchestrationStore } from '@/store/orchestrationStore';
import { AppCreateTypeEnum } from '@/common/app/constants';


// 默认图像映射
export const DEFAULT_IMAGES: Record<string, string> = {
  default: agentLogo1,
  defaultLogo1: logo1,
  defaultLogo2: logo2,
  aiaalogo: aiaalogo,
  edu: homeback,
  ent: entImg,
  med: medImg,
  aiaa: aiaaImg,

  agentLogo1: agentLogo1,
  agentLogo2: agentLogo2,
  agentLogo3: agentLogo3,
  workflowLogo1: workflowLogo1,
  workflowLogo2: workflowLogo2,
  workflowLogo3: workflowLogo3,
  mcpLogo1: mcpLogo1,
  mcpLogo2: mcpLogo2,
  mcpLogo3: mcpLogo3,
  fusionLogo1: fusionLogo1,
  fusionLogo2: fusionLogo2,
  fusionLogo3: fusionLogo3,
  human: human,

};
export const CUSTOM_IMAGES: Record<string, string> = {
  firecrawl: firecrawl,
  amap: amap,
  baidumap: baidumap,
  tencentmap: tencentmap,
  deepwiki: deepwiki,
  gitmcp: gitmcp,
  huggingface: huggingface,
};

export const DEFAULT_LOGOS: Record<string, string> = {
  default: logo,
  defaultLogo1: logo1,
  defaultLogo2: logo2,
  aiaalogo: aiaalogo,

  agentLogo1: agentLogo1,
  agentLogo2: agentLogo2,
  agentLogo3: agentLogo3,
  workflowLogo1: workflowLogo1,
  workflowLogo2: workflowLogo2,
  workflowLogo3: workflowLogo3,
  mcpLogo1: mcpLogo1,
  mcpLogo2: mcpLogo2,
  mcpLogo3: mcpLogo3,
  fusionLogo1: fusionLogo1,
  fusionLogo2: fusionLogo2,
  fusionLogo3: fusionLogo3,

};



export const DEFAULT_SCENES: Record<string, string> = {
  edu: homeback,
  ent: entImg,
  med: medImg,
  aiaa: aiaaImg,
};


interface ImageDisplayProps {
  src: string;
  alt?: string;
  isSelected?: boolean;
  onClick?: () => void;
  className?: string;
  containerClassName?: string;
  objectFit?: 'cover' | 'contain' | 'fill' | 'none' | 'scale-down';
  defaultImage?: string;
  appType?: string;
}

/**
 * 通用图像展示组件，支持显示URL、base64和默认图像
 */
const ImageDisplay: React.FC<ImageDisplayProps> = ({
  src,
  alt = '图片',
  isSelected = false,
  onClick,
  className = '',
  containerClassName = '',
  objectFit = 'cover',
  defaultImage,
  appType,
}) => {
  const { special_app_lable } = useOrchestrationStore();


  // 处理图像源
  let imageSource = src;

  // 如果是预设的ID，从默认图像映射中获取
  if (DEFAULT_IMAGES[src]) {
    imageSource = DEFAULT_IMAGES[src];
  }
  if (src === '/icon/human.svg') {
    imageSource = human;
  }
  // 如果是自定义的ID，从自定义图像映射中获取
  if (CUSTOM_IMAGES[src]) {
    imageSource = CUSTOM_IMAGES[src];
  }
  // 如果提供了默认图像且src是'default'
  else if (src === 'default' && defaultImage) {
    // 检查defaultImage是否是预设ID
    imageSource = DEFAULT_IMAGES[defaultImage] || defaultImage;
  }

  const isSvg = (str: string): boolean => {
    return str.endsWith('.svg');
  };

  // 简化的base64检测逻辑
  const isBase64 = (str: string): boolean => {
    if (!str) return false;
    if (str.startsWith('http')) return false;

    // 检查是否为内置资源引用的路径
    if (Object.values(DEFAULT_IMAGES).includes(str)) return false;

    // 简单判断是否符合base64格式
    return /^[A-Za-z0-9+/=]+$/.test(str);
  };

  // 处理base64格式的图像
  if (imageSource && isBase64(imageSource)) {
    imageSource = `data:image/png;base64,${imageSource}`;
  }

  // 是否有效的图像URL或base64
  const isValidImage = !!imageSource

  return (
    <div
      className={`border ${isSelected ? `border-primary ${appType === AppCreateTypeEnum.mcp ? 'border' : 'border-2'}` : 'border-[#E5E6EB]'
        } overflow-hidden cursor-pointer ${containerClassName}`}
      onClick={onClick}
    >
      <div className="relative h-full flex justify-center items-center">
        {isValidImage ?
          isSvg(src!) ? (
            <div className={`${appType === AppCreateTypeEnum.mcp ? 'h-6 w-6' : 'w-full h-full'} flex items-center justify-center`}>
              <SvgIcon
                icon={getFilenameFromPath(src!).replace('.svg', '').replace('.svg', '')}
                size={64}
              />
            </div>
          ) : (
            <img
              src={imageSource}
              alt={alt}
              className={`${appType === AppCreateTypeEnum.mcp ? 'h-6 rounded-small' : 'h-full'} object-${objectFit} ${className}`}
            />
          ) : (
            <div className="w-full h-full flex items-center justify-center bg-gray-100">
              <span className="text-gray-400">无图像</span>
            </div>
          )}
      </div>
    </div>
  );
};

export default ImageDisplay; 